package com.example.asus.mydiary;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.example.asus.Test.Diary;
import com.example.asus.db.domain.Note;
import com.example.asus.db.gen.NoteDao;
import com.example.asus.db.gen.PasswordDao;
import com.example.asus.db.service.GreenDaoManager;
import com.example.asus.db.service.SysApplication;

import java.util.ArrayList;
import java.util.List;

public class DiaryRecycler extends AppCompatActivity {

    RecyclerView recyclerView = null;
    private Note note;
    private List<Note> noteData = null;
    private NoteDao noteDao;
    private PasswordDao passwordDao;
    private DiaryRecyclerAdapter diaryRecyclerAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary_recycler);
        SysApplication.getInstance().addActivity(this);
        /**
         *  ToolBar 设置
         */
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        toolbar.inflateMenu(R.menu.toorbarmenu_diary);
//        toolbar.setTitle("我的日记");
//        toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));

        initView();
        initData();

    }

    /**
     *  初始化界面控件
     */
    public void initView(){
        recyclerView = (RecyclerView)findViewById(R.id.diary_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    /**
     *  初始化数据
     */
    public void initData(){
        noteData = new ArrayList<Note>();
        GreenDaoManager.InitDao(this,"diary.db");
        noteDao = GreenDaoManager.getDaoSession().getNoteDao();
        noteData = noteDao.loadAll();  //读取当前数据库所有数据

        DiaryRecyclerAdapter diaryRecyclerAdapter = new DiaryRecyclerAdapter(noteData);
//        diaryRecyclerAdapter.notifyDataSetChanged();
        recyclerView.setAdapter(diaryRecyclerAdapter);   //设置适配器

        /**
         *  监听点击事件
         *  获得当前条目的位置，positon 与 数据库数据所在的位置相同，根据position找到对应的id
         *  而不是position = id+1，因为当删除或修改之后就不存在这样的关系了
         */
        diaryRecyclerAdapter.setOnItemClickListener(new DiaryRecyclerAdapter.ItemClickListener() {
            Note currentNote = new Note();

            @Override
            public void onItemClick(int position) {
                // Test
//                Toast.makeText(DiaryRecycler.this, "条目位置：" + position, Toast.LENGTH_SHORT).show();

                // 查找数据库对于数据位置
                currentNote = noteDao.loadByRowId(noteData.get(position).getId());
                Intent intent = new Intent(DiaryRecycler.this,View_and_Edit_Diary.class);
                /**
                 *  向另一个活动传递数据的三种方法
                 *  1. 使用Intent直接传递，这种方法只能传递一般类型的数据，不能传递对象
                 *  2. Serializable 序列化
                 *  3. 实现 Parcelable 接口
                 *  方法2 和 3 可以传递对象，其中方法3 效率更高，推荐使用
                 */
                intent.putExtra("currentData",currentNote);
                startActivity(intent);

            }

            /**
             * 删除按钮
             * position为当前条数
             * @param position
             */
            @Override
            public void onDeleteItemClick(final int position) {

                final AlertDialog.Builder deleteDialog = new AlertDialog.Builder(DiaryRecycler.this);

                deleteDialog.setIcon(R.drawable.btn_delete1);
                deleteDialog.setTitle("是否确定删除？");
//        deleteDialog.setMessage("你确定要删除这条记录？");

                deleteDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // to do
                        noteDao.deleteByKey(noteData.get(position).getId());
                        /**
                         *  返回到日记页面
                         */

                        Intent intent = new Intent(DiaryRecycler.this,DiaryRecycler.class);
                        startActivity(intent);
                        finish();

                    }
                });
                deleteDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // to do
                    }
                });
                deleteDialog.show();

            }
        });

    }

    /**
     *  初始化菜单
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_diary,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_add:
//                Toast.makeText(DiaryRecycler.this,"Clicked add",Toast.LENGTH_LONG).show();
                Intent intent = new Intent(DiaryRecycler.this,AddDiary.class);
                startActivity(intent);
//                finish();
                break;
            case R.id.menu_exit:
                SysApplication.getInstance().exit();
                break;
            case R.id.menu_psw:
                GreenDaoManager.InitDao(this,"password.db");
                passwordDao = GreenDaoManager.getDaoSession().getPasswordDao();
                passwordDao.deleteAll();  //删除密码

                Intent intent_reset = new Intent(DiaryRecycler.this,FirstTime.class);
                startActivity(intent_reset);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     *  后退菜单栏
     *  不管怎样在主页面都直接退出
     */
    @Override
    public void onBackPressed() {
        SysApplication.getInstance().exit();
    }


    public void userChoosed(String choise){
        if(choise == "Answer.YES"){
            Toast.makeText(DiaryRecycler.this, "YOU CHOICED YES", Toast.LENGTH_LONG).show();
        }
        else if (choise == "Answer.No"){
            Toast.makeText(DiaryRecycler.this, "YOU CHOICED NO", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 删除对话框
     */
    private void showDeleteDialog() {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder deleteDialog = new AlertDialog.Builder(DiaryRecycler.this);

        deleteDialog.setIcon(R.drawable.btn_delete1);
        deleteDialog.setTitle("是否确定删除？");
//        deleteDialog.setMessage("你确定要删除这条记录？");

        deleteDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // to do
            }
        });
        deleteDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // to do
            }
        });
        deleteDialog.show();
    }
}
